using System;
using System.Collections;

namespace popLib
{

	public class NumberLib
	{
		
		static string hexa = "202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E";
		static string text = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
				
	
		//-------------------Convert function-----------------
	    
		/// <summary>
		/// get Decimal (A B -> 65 32 66)
		/// var is a input string, midstr is a string between two decimals
		/// (midstr = " " -->A B -> 65 32 66
		/// midstr = " 00 " -->A B -> 65 00 32 00 66 00)
		/// </summary>
		public static  string getDecimal(string var,string midstr)//get Decimal (A B -> 65 32 66)
		{
			/// <summary>
			/// Lib1 include some functions about string, hexadecimal, decimal, binary...
			/// </summary>
			string rs = "";
			string sp = midstr;
	
			for(int i=0;i<var.Length;i++)
			{
				if(text.IndexOf(var.Substring(i,1)) >=0 )
					rs += (text.IndexOf(var.Substring(i,1)) + 32) + sp ;
			}
			return rs;
		}
	
		
		public static  string deDecimal(string var,bool addOutChar)//get text from decimal (65 32 66 --> A B )
		{
			string	rs  = "";
			int		buf = 0;			
			
			if(var.Replace(" ","").Length > 0)
			{
				var = StringLib.filterStr(var," 0123456789");
			
				if(var.IndexOf(" ") >= 0)
				{
					string[] arr = var.Split(' ');
					for(int i=0;i<arr.Length;i++)
					{
						if(arr[i] == "") buf = -1;
						else	buf = Convert.ToInt32(arr[i]);
	
						if( buf >= 32 && buf-32 <= 94)
							rs += text.Substring( buf - 32 , 1 ) + "";
						else if(addOutChar && buf >=0 )
							rs += "[" + buf + "]" ;											
					}
				}
				else if( getInt(var) >= 32 && getInt(var)-32 <= 94)
					rs = text.Substring(getInt(var) - 32,1);
				else if(addOutChar)
					rs = "[" + getInt(var) + "]" ;
			}
	
			return rs;
		}
		//End
	
	
		//Hexadecimal
		public static  string getHexa(string var,bool addMidStr)//get hexadecimal (LM -> 4B 4D)
		{
			string rs = "";
			string sp = "";
			for(int i=0;i<var.Length;i++)
			{
				if(i==var.Length-1)
					sp = "";
				else
					sp = " ";
	
				if(addMidStr)
					rs += hexa.Substring( (text.IndexOf(var.Substring(i,1) ) * 2), 2 )+" 00" + sp;
				else
					rs += hexa.Substring( (text.IndexOf(var.Substring(i,1) ) * 2), 2 )+sp;					
			}
			
			return rs;
		}
	
		public static  string deHexa(string var,bool addOutHex)//get text from hexadecimal (4B 4D -> LM)
		{
			string rs  = "";
			string buf = "";
			bool   foundit = false;
			var = var.Replace(" ","");
			
			if( var.Length%2 == 1)
				var = var.Substring(0,var.Length-1);
	
			for(int i=0;i<var.Length;i+=2)
			{
				buf = var.Substring(i,2).ToUpper();
				for(int j=0;j<95;j++)
				{
					if( hexa.Substring(j*2,2).Equals(buf))
					{
						rs += text.Substring(j,1);
						foundit = true;
						break;
					}
					else 
						foundit = false;
				}		
					if(addOutHex && !foundit)
						rs += "["+ buf +"]";
				
			}
			return rs;
		}
		//end
	
	
		//Binary
		public static  string getBinary(string var1)//get binary (12 -> 1100)
		{
			int var = getInt(var1);
			string rs = "";
			while( var !=0 )
			{
				rs += var % 2 ;
				var = var/2;
			}
			string rs1 = "";
			for(int i=rs.Length-1;i>-1;i--)
				rs1+=rs.Substring(i,1);			
			return rs1;
		}
	
		public static  string deBinary(string var)//get text from binary (1100 -> 12)
		{	
			var = StringLib.filterStr(var,"01");			
			int rs = 0;
			for(int i=0;i<var.Length;i++)
				rs = (rs + Convert.ToInt16(var.Substring(i,1)) )*2;
			
			return rs/2 + "";			
		}
		//End
	
		//----------------End Convert function-----------------
		
		//Return number only
		public static int getInt(object obj)
		{				
			if( obj == null ) return 0;
			return Convert.ToInt32( fixNumber( obj.ToString() ).Split('.')[0] );
		}	
		
		//Return number only
		public static long getInt64(object obj)
		{				
			if( obj == null ) return 0;
			return Convert.ToInt64( fixNumber( obj.ToString() ).Split('.')[0] );
		}
		
		//Return number only
		public static double getDouble(object obj)
		{	
			if( obj == null ) return 0;
			return Convert.ToDouble( fixNumber( obj.ToString() ) );
		}
				
		public static double getDouble( object obj, int decimalLength ){
			if( obj == null ) return 0;
			string number = fixNumber( obj.ToString() );
			int dotIndexer = number.IndexOf(".");
			
			if( dotIndexer >= 0 && number.Length - dotIndexer > decimalLength+1 )
				number = number.Substring( 0, dotIndexer+decimalLength+1 );
			return Convert.ToDouble( number );
		}
		
		public static string fixNumber( object obj ){
			string number = StringLib.filterStr( obj.ToString(), "-0123456789."  );
			
			if( number.IndexOf( "-" ) >= 0 ){
				if( number.StartsWith("-") ) number = "=" + number;
				number = number.Replace("-","").Replace("=","-");
			}
			
			if( number.IndexOf( "." ) >= 0 ){
				if( number.StartsWith(".") ) number = "0" + number;
				if( number.EndsWith(".") )   number = number.Substring(0,number.Length-1);
				number = StringLib.Replace1time( number, ".", "*" ).Replace( ".", "").Replace("*",".");
			}
			
			if( (number.IndexOf("-") == 0 && number.Length == 1) || number == "-0" || number.Length == 0 ) return "0";			
			
			return number;
		}
		
				
		public static DateTimeLib.CompareType compareNumber(string _num1, string _num2){
			double num1 = getDouble(_num1);
			double num2 = getDouble(_num2);
			
			if( num1 > num2 ) return DateTimeLib.CompareType.higher;
			if( num1 < num2 ) return DateTimeLib.CompareType.lower;
			return DateTimeLib.CompareType.equal;
		}
		
		public static string formatMoney(object money, int nfloat){
			double dmoney = getDouble(money);
			if (dmoney <= 0.001)
				dmoney = 0;
			string fpoint = "";
			if (nfloat > 0)
			{
				fpoint = ".";
				while (nfloat > 0)
				{
					fpoint += "0";
					nfloat--;
				}
			}
			return dmoney.ToString( "#,##0" + fpoint);			
		}
		
		public static string formatMoney(object money){
			return formatMoney(money, 0);
		}

	}
}

